package com.hangwei.paperless.fragment.sub;

import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.hangwei.paperless.R;
import com.hangwei.paperless.SubRepairActivity;
import com.hangwei.paperless.adapter.HorizontalPicAdapter;
import com.hangwei.paperless.adapter.sub.SubProcessListAdapter;
import com.hangwei.paperless.adapter.sub.SubProcessListStepAdapter;
import com.hangwei.paperless.model.bean.CardToSeal;
import com.hangwei.paperless.model.bean.DataFilling;
import com.hangwei.paperless.model.bean.SubProcessListAll;
import com.hangwei.paperless.model.bean.SubProcessListInfo;
import com.hangwei.paperless.presenter.StaticPresenter;
import com.hangwei.paperless.utils.JsonPostUtil;
import com.hangwei.paperless.utils.NFCCallBack;
import com.twelve.all.base.LoadFragment;
import com.twelve.all.net.CallBack;
import com.twelve.all.utils.LoadingDialog;
import com.twelve.all.utils.SPKey;
import com.twelve.all.utils.SPUtil;
import com.twelve.all.utils.ToastUtil;
import com.twelve.all.utils.Tools;

import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by 张龙臣 on 2020/11/27.
 * 衬套加工单
 */

public class SubProcessListFragment extends LoadFragment {
    private String mBillId;
    private SubRepairActivity mActivity;
    private SubProcessListAdapter mAdapter;
    private String mSubId;
    private DataFilling mDataFilling;
    private String mUserId;
    private SubProcessListStepAdapter mStepAdapter;
    private TextView mTvManualNo;
    private TextView mTvBushPN;
    private TextView mTvBushIPLNo;
    private TextView mTvMaterial;
    private TextView mTvQty;
    private TextView mTvDes;
    private HorizontalPicAdapter mPicAdapter;
//    private MainBottomPopupWindow mMainBottomPopupWindow;
    private RecyclerView mRecyclerView;
    private String mSubBillId;
    private TextView mTvJobNo;
    private TextView mTvSheetNo;

    @Override
    protected View onRealCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Bundle arguments = getArguments();
        if (arguments != null) {
            mBillId = arguments.getString("billId");
            mSubId = arguments.getString("id");
            mDataFilling = (DataFilling) arguments.getSerializable("data");
            mActivity = (SubRepairActivity) getActivity();
            mUserId = SPUtil.getString(SPKey.Account.USER_ID);
        }
        View inflate = inflater.inflate(R.layout.fragment_sub_process_list, container, false);
        mRecyclerView = inflate.findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        mAdapter = new SubProcessListAdapter(null) {
            @Override
            protected void seal(final SubProcessListInfo item, final List<SubProcessListInfo> data) {
                mActivity.sealByCard(item.id, new NFCCallBack() {
                    @Override
                    public void onCallBack(List<CardToSeal.StampBean> stampIds, String userId) {
                        if (stampIds==null||stampIds.size()==0){
                            ToastUtil.showCenterToast("无印章！");
                        }else {
                            final LoadingDialog loadingDialog = new LoadingDialog(mActivity);
                            loadingDialog.show();
                            String jsonData = JsonPostUtil.buildProcessList(mUserId, data,item,stampIds.get(0).stampId,userId);
                            RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData);
                            StaticPresenter.stampBushingSheetDRS(requestBody, new CallBack<Object>() {
                                @Override
                                public void onCompleted(Object o) {
                                    loadingDialog.dismiss();
                                    initData();
//                                    mMainBottomPopupWindow.dismiss();
                                }

                                @Override
                                public void onError() {
                                    loadingDialog.dismiss();
                                }
                            });
                        }
                    }
                });
//                if (App.stamps != null && App.stamps.size() > 0) {
//                    //有印章
//                    View inflate = LayoutInflater.from(mActivity).inflate(R.layout.pop_seal, null);
//                    mMainBottomPopupWindow = new MainBottomPopupWindow(mActivity, inflate, BottomPopType.Seal);
//                    mMainBottomPopupWindow.setNeedAlpha(false);
//                    mMainBottomPopupWindow.setOnSealListener(new MainBottomPopupWindow.OnSealListener() {
//                        @Override
//                        public void onSeal(String stampId) {
//                            final LoadingDialog loadingDialog = new LoadingDialog(mActivity);
//                            loadingDialog.show();
//                            String jsonData = JsonPostUtil.buildProcessList(mUserId, data,item,stampId);
//                            RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData);
//                            StaticPresenter.stampBushingSheetDRS(requestBody, new CallBack<Object>() {
//                                @Override
//                                public void onCompleted(Object o) {
//                                    loadingDialog.dismiss();
//                                    initData();
//                                    mMainBottomPopupWindow.dismiss();
//                                }
//
//                                @Override
//                                public void onError() {
//                                    loadingDialog.dismiss();
//                                }
//                            });
//                        }
//                    });
//                    mMainBottomPopupWindow.showAtLocation(mRecyclerView, Gravity.BOTTOM, 0, 0);
//                } else {
//                    ToastUtil.showCenterToast("无印章");
//                }
            }
        };
        mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tvSave) {
                    SubProcessListAll.DrsListBean bean = mAdapter.getData().get(position);
                    final LoadingDialog loadingDialog = new LoadingDialog(mActivity);
                    loadingDialog.show();
                    StaticPresenter.saveBushingSN(mUserId, mSubBillId, bean.billSeq, bean.bushingSN, new CallBack<Object>() {
                        @Override
                        public void onCompleted(Object o) {
                            loadingDialog.dismiss();
                            ToastUtil.showCenterToast("保存成功！");
//                            initData();
                        }

                        @Override
                        public void onError() {
                            loadingDialog.dismiss();
                            ToastUtil.showCenterToast("保存失败！");
                        }
                    });
                }
            }
        });
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.addHeaderView(getHeader());
        mAdapter.addFooterView(getFooter());
        initData();
        return inflate;
    }

    private void initData() {
        final LoadingDialog loadingDialog = new LoadingDialog(mActivity);
        loadingDialog.show();
        StaticPresenter.getBushingSheets(mUserId, mBillId, mSubId, null, new CallBack<SubProcessListAll>() {
            @Override
            public void onCompleted(SubProcessListAll subProcessListAll) {
                loadingDialog.dismiss();
                if (subProcessListAll.state==0){
                    mActivity.showTrans();
                }
                mAdapter.setNewData(subProcessListAll.drsList);
                mActivity.setStepId(subProcessListAll.id);
                if (subProcessListAll.stepList != null && subProcessListAll.stepList.size() > 0) {
                    for (int i = 0; i < subProcessListAll.stepList.size(); i++) {
                        SubProcessListAll.StepListBean bean = subProcessListAll.stepList.get(i);
                        if (TextUtils.isEmpty(bean.technicianId)) {
                            bean.canSeal = true;
                            break;
                        }
                    }
                }
                mStepAdapter.setNewData(subProcessListAll.stepList);
                mSubBillId = subProcessListAll.subBillId;
                mTvManualNo.setText(Tools.checkedString(subProcessListAll.manualNo));
                mTvBushPN.setText(Tools.checkedString(subProcessListAll.bushPN));
                mTvBushIPLNo.setText(Tools.checkedString(subProcessListAll.bushIPLNo));
                mTvMaterial.setText(Tools.checkedString(subProcessListAll.material));
                mTvQty.setText(Tools.checkedString(subProcessListAll.qty));
                String source = Tools.checkedString(subProcessListAll.description);
                String all = source.replaceAll("<p>", "<p  style=\"display:block;margin:0 0;\">");
                Spanned html;
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N)
                    html = Html.fromHtml(all, Html.FROM_HTML_MODE_COMPACT);
                else html = Html.fromHtml(all);
                mTvDes.setText(html);
                mTvJobNo.setText(Tools.checkedString(subProcessListAll.woNo));
                mTvSheetNo.setText(Tools.checkedString(subProcessListAll.sheetNo));
            }

            @Override
            public void onError() {
                loadingDialog.dismiss();
            }
        });
    }

    private View getHeader() {
        View inflate = LayoutInflater.from(mActivity).inflate(R.layout.header_sub_process_list, null);
        mTvJobNo = inflate.findViewById(R.id.tvJobNo);
        mTvSheetNo = inflate.findViewById(R.id.tvSheetNo);
        mTvManualNo = inflate.findViewById(R.id.tvManualNo);
        mTvBushPN = inflate.findViewById(R.id.tvBushPN);
        mTvBushIPLNo = inflate.findViewById(R.id.tvBushIPLNo);
        mTvMaterial = inflate.findViewById(R.id.tvMaterial);
        mTvQty = inflate.findViewById(R.id.tvQty);
        mTvDes = inflate.findViewById(R.id.tvDes);
        return inflate;
    }

    private View getFooter() {
        View inflate = LayoutInflater.from(mActivity).inflate(R.layout.footer_sub_process_list, null);
        RecyclerView recyclerViewStep = inflate.findViewById(R.id.recyclerViewStep);
        recyclerViewStep.setLayoutManager(new LinearLayoutManager(mActivity));
        mStepAdapter = new SubProcessListStepAdapter(null) {
            @Override
            protected void seal(final String id) {
                mActivity.sealByCard(id, new NFCCallBack() {
                    @Override
                    public void onCallBack(List<CardToSeal.StampBean> stampIds, String userId) {
                        if (stampIds==null||stampIds.size()==0){
                            ToastUtil.showCenterToast("无印章！");
                        }else {
                            final LoadingDialog loadingDialog = new LoadingDialog(mActivity);
                            loadingDialog.show();
                            StaticPresenter.stampBushingSheetStep(mUserId, id, userId, stampIds.get(0).stampId, new CallBack<Object>() {
                                @Override
                                public void onCompleted(Object o) {
                                    loadingDialog.dismiss();
                                    initData();
                                }

                                @Override
                                public void onError() {
                                    loadingDialog.dismiss();
                                }
                            });
                        }
                    }
                });
//                if (App.stamps != null && App.stamps.size() > 0) {
//                    //有印章
//                    View inflate = LayoutInflater.from(mActivity).inflate(R.layout.pop_seal, null);
//                    mMainBottomPopupWindow = new MainBottomPopupWindow(mActivity, inflate, BottomPopType.Seal);
//                    mMainBottomPopupWindow.setNeedAlpha(false);
//                    mMainBottomPopupWindow.setOnSealListener(new MainBottomPopupWindow.OnSealListener() {
//                        @Override
//                        public void onSeal(String stampId) {
//                            final LoadingDialog loadingDialog = new LoadingDialog(mActivity);
//                            loadingDialog.show();
//                            StaticPresenter.stampBushingSheetStep(mUserId, id, mUserId, stampId, new CallBack<Object>() {
//                                @Override
//                                public void onCompleted(Object o) {
//                                    loadingDialog.dismiss();
//                                    initData();
//                                    mMainBottomPopupWindow.dismiss();
//                                }
//
//                                @Override
//                                public void onError() {
//                                    loadingDialog.dismiss();
//                                }
//                            });
//                        }
//                    });
//                    mMainBottomPopupWindow.showAtLocation(mRecyclerView, Gravity.BOTTOM, 0, 0);
//                } else {
//                    ToastUtil.showCenterToast("无印章");
//                }
            }
        };
        recyclerViewStep.setAdapter(mStepAdapter);

        RecyclerView recyclerViewPic = inflate.findViewById(R.id.recyclerViewPic);
        LinearLayoutManager layout = new LinearLayoutManager(mActivity);
        layout.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerViewPic.setLayoutManager(layout);
        mPicAdapter = new HorizontalPicAdapter(mDataFilling == null ? null : mDataFilling.picList);
        recyclerViewPic.setAdapter(mPicAdapter);
        recyclerViewPic.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.sdv)
                    mActivity.showBigPic((ArrayList<String>) mPicAdapter.getData(), position);
            }
        });
        return inflate;
    }
}
